<template>
<div>
  <el-dialog
    :title="title"
    :close-on-click-modal="false"
     v-dialogDrag
    :visible.sync="visible">
    <el-form :model="inputForm" ref="inputForm" v-loading="loading" :class="method==='view'?'readonly':''"  :disabled="method==='view'"
             label-width="120px">
      <el-row  :gutter="15">
        <el-col :span="12">
          <el-form-item label="等级" prop="level"
                        :rules="[
                 ]">
            <el-input v-model="inputForm.level" placeholder="请填写等级"     ></el-input>
          </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="等级名称" prop="title"
                :rules="[
                 ]">
              <el-input v-model="inputForm.title" placeholder="请填写等级名称"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="等级积分区间起点" prop="inteStart"
                :rules="[
                 ]">
              <el-input v-model="inputForm.inteStart" placeholder="请填写等级积分区间起点"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="12">
            <el-form-item label="等级积分区间终点" prop="inteOver"
                :rules="[
                 ]">
              <el-input v-model="inputForm.inteOver" placeholder="请填写等级积分区间终点"     ></el-input>
           </el-form-item>
        </el-col>
        <el-col :span="24">
            <el-form-item label="活动规则说明" prop="content"
                :rules="[
                 ]">
            <WangEditor ref="contentEditor" v-model="inputForm.content"/>
           </el-form-item>
        </el-col>
        </el-row>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">关闭</el-button>
      <el-button type="primary" v-if="method != 'view'" @click="doSubmit()" v-noMoreClick>确定</el-button>
    </span>
  </el-dialog>
</div>
</template>

<script>
  import WangEditor from '@/components/editor/WangEditor'
  export default {
    data () {
      return {
        title: '',
        method: '',
        visible: false,
        loading: false,
        inputForm: {
          id: '',
          title: '',
          inteStart: '',
          inteOver: '',
          level: '',
          content: ''
        }
      }
    },
    components: {
      WangEditor
    },
    methods: {
      init (method, id) {
        this.method = method
        this.inputForm.id = id
        if (method === 'add') {
          this.title = `新建会员配置`
        } else if (method === 'edit') {
          this.title = '修改会员配置'
        } else if (method === 'view') {
          this.title = '查看会员配置'
        }
        this.visible = true
        this.loading = false
        this.$nextTick(() => {
          this.$refs.inputForm.resetFields()
          this.$refs.contentEditor.init('')
          if (method === 'edit' || method === 'view') { // 修改或者查看
            this.loading = true
            this.$http({
              url: `/memberconfig/memberConfig/queryById?id=${this.inputForm.id}`,
              method: 'get'
            }).then(({data}) => {
              this.inputForm = this.recover(this.inputForm, data.memberConfig)
              this.$refs.contentEditor.init(this.inputForm.content)
              this.loading = false
            })
          }
        })
      },
      // 表单提交
      doSubmit () {
        this.$refs['inputForm'].validate((valid) => {
          if (valid) {
            this.loading = true
            this.$http({
              url: `/memberconfig/memberConfig/save`,
              method: 'post',
              data: this.inputForm
            }).then(({data}) => {
              this.loading = false
              if (data && data.success) {
                this.visible = false
                this.$message.success(data.msg)
                this.$emit('refreshDataList')
              }
            })
          }
        })
      }
    }
  }
</script>
